<!DOCTYPE html>
<html>
	<head>
		<include file="Public/head" />
		<link rel="stylesheet" href="__PUBLIC__/Manage/css/table.css">
		<script src="__PUBLIC__/Manage/js/wangEditor/wangEditor.min.js"></script>
		<title>编辑自由块</title>
	</head>
	<body>
		<div class="nestable">
			<div class="console-title console-title-border drds-detail-title clearfix">
				<h5>自由块修改</h5>
			</div>
			<div class="public-selectArea public-selectArea1">
				<form action="{:U('Block/edit',array('id'=>$data['id']))}" method="post">
					<div class="clearfix">
						<div class="wp_box  col-xs-8">
							<dl>
								<dt>调用名称：</dt>
								<dd>
									<input type="text" name="name" value="{$data.name}">
								</dd>
							</dl>
						</div>
					</div>
					<div class="clearfix">
						<div class="wp_box  col-xs-8">
							<dl>
								<dt>备注：</dt>
								<dd>
									<input type="text" name="remarks" value="{$data.remarks}">
								</dd>
							</dl>
						</div>
					</div>
					<div class="clearfix">
						<div class="wp_box  col-xs-8">
							<dl>
								<dt>类型：</dt>
								<dd>
		                          	<select class="select" name="type">
		                                <option value="1" <if condition="$data['type'] eq 1"> selected="selected"</if> >纯文本</option>
		                                <option value="2" <if condition="$data['type'] eq 2"> selected="selected"</if> >图片</option>
		                                <option value="3" <if condition="$data['type'] eq 3"> selected="selected"</if> >富文本</option>
		                            </select>
								</dd>
							</dl>
						</div>
					</div>
					<div id="content">
						<div id="content-text"><textarea name="content" rows="8" style="width: 60%;margin-left: 130px;">{$data.content}</textarea></div>
					</div>
					<div class="btnArea">
						<a href="javascript:;" class="btn btn-sereachBg" id="submitBtn">
							<span class="public-label">提交</span>
						</a>
					</div>
				</form>
			</div>
		</div>
	</body>
	<script>
		$(function(){
			var E = window.wangEditor;
			var editor;
			var type = $("select[name='type']").val();
			if(type == 1){
				$("#content-html").remove();
				$("#content-img").remove();
				$("#content-text").remove();
				$("#content").html('<div id="content-text"><textarea name="content" rows="8" style="width: 60%;margin-left: 130px;">{$data.content}</textarea></div>');
			}else if(type == 2){
				$("#content-html").remove();
				$("#content-img").remove();
				$("#content-text").remove();
				$("#content").html('<div class="clearfix" id="content-img"><div class="wp_box  col-xs-8"><dl><dt>图片地址：</dt><dd><input type="text" name="content" value="{$data.content}"></dd></dl></div></div>');
			}else{
				$("#content-html").remove();
				$("#content-img").remove();
				$("#content-text").remove();
				$("#content").html('<div id="content-html"><div id="edit" style="width: 60%;margin-left: 130px;">{$data.content}</div><input type="hidden" name="content" value="{$data.content}" /></div>');
				editor = new E('#edit');
				editor.create();
			}
			$("select[name='type']").on('change',function(){
				var type = $(this).val();
				if(type == 1){
					$("#content-html").remove();
					$("#content-img").remove();
					$("#content-text").remove();
					$("#content").html('<div id="content-text"><textarea name="content" rows="8" style="width: 60%;margin-left: 130px;">{$data.content}</textarea></div>');
				}else if(type == 2){
					$("#content-html").remove();
					$("#content-img").remove();
					$("#content-text").remove();
					$("#content").html('<div class="clearfix" id="content-img"><div class="wp_box  col-xs-8"><dl><dt>图片地址：</dt><dd><input type="text" name="content" value="{$data.content}"></dd></dl></div></div>');
				}else{
					$("#content-html").remove();
					$("#content-img").remove();
					$("#content-text").remove();
					$("#content").html('<div id="content-html"><div id="edit" style="width: 60%;margin-left: 130px;">{$data.content}</div><input type="hidden" name="content" value="{$data.content}" /></div>');
					editor = new E('#edit');
					editor.create();
				}
			});
	        $("#submitBtn").on('click',function(){
	        	var type = $("select[name='type']").val();
	        	if(type == 3) $("input[name='content']").val(editor.txt.html());
	        	
	        	var name = $("input[name='name']").val();
	        	if(name.length == 0){
	        		layer.msg("请输入调用名称");
	        		return ;
	        	}
	        	cvphp.submit($("form"),function(data){
	        		if(data.status!=1){
	        			layer.msg(data.info);
	        		}else{
	        			layer.msg('保存成功');
	        			setTimeout(function(){
	        				window.location.href="{:U('Block/index')}";
	        			},2000);
	        		}
	        	});
	        });
	        
	        
		});
	</script>
</html>